package com.xzh.wms.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.xzh.smart.core.exception.BizException;
import com.xzh.wms.dao.InventoryBatchDao;
import com.xzh.wms.dao.InventoryDao;
import com.xzh.wms.dao.IoItemDao;
import com.xzh.wms.dto.InventoryOptionDTO;
import com.xzh.wms.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service("in")
public class InInventoryOptionServiceImpl implements IInventoryOptionService{

    @Autowired
    private IoItemDao ioItemDao;
    @Autowired
    private InventoryDao inventoryDao;
    @Autowired
    private InventoryBatchDao inventoryBatchDao;

    @Override
    public void process(InventoryOptionDTO dto) {
        String id = dto.getRefId();
        IoItemQuery query = new IoItemQuery().builder().ioId(id).build();
        List<IoItem> ioItems = ioItemDao.select(query);
        ioItems.forEach(item->{
            InventoryQuery query1 = new InventoryQuery().builder().id(item.getProductId()).build();
            List<Inventory> inventories = inventoryDao.select(query1);
            if (ObjectUtil.isEmpty(inventories)){
                Inventory inventory = new Inventory().builder()
                        .id(item.getProductId())
                        .productName(item.getProductName())
                        .qty(item.getQty())
                        .saleCount(0)
                        .version(1)
                        .build();
                inventoryDao.insert(inventory);
            }else {
                Inventory inventory = inventories.get(0);
                Integer i = inventoryDao.updateQty(item.getQty(), item.getProductId(), inventory.getVersion());
                if (i==0){
                    throw new BizException(454, "库存更新失败");
                }
            }
            String batchId = "B" + item.getProductId() + "-" + LocalDateTimeUtil.format(item.getProductionDate(), "yyyyMMdd");
            InventoryBatchQuery query2 = new InventoryBatchQuery().builder().batchId(batchId).productId(item.getProductId()).build();
            List<InventoryBatch> inventoryBatches = inventoryBatchDao.select(query2);
            if (ObjectUtil.isEmpty(inventoryBatches)){
                Date date = DateUtil.date(item.getProductionDate());
                DateTime dateTime = DateUtil.offsetDay(date, item.getShelfLife());
                InventoryBatch inventoryBatch = new InventoryBatch().builder()
                        .id(item.getProductId())
                        .batchId(batchId)
                        .productId(item.getProductId())
                        .productName(item.getProductName())
                        .qty(item.getQty())
                        .shelfId(item.getShelfId())
                        .shelfLife(item.getShelfLife())
                        .productionDate(item.getProductionDate())
                       .expirationDate(dateTime.toLocalDateTime().toLocalDate())
                        .brief(item.getBrief())
                        .version(0)
                        .build();
                inventoryBatchDao.insert(inventoryBatch);
            }else {
                InventoryBatch inventoryBatch = inventoryBatches.get(0);
                Integer i = inventoryBatchDao.updateQty(item.getQty(), batchId, inventoryBatch.getVersion());
                if (i==0){
                    throw new BizException(454, "库存批次更新失败");
                }
            }
        });
    }
}
